#include "curvetestpage.h"
#include "eventid.h"
#include "application.h"

CurveTestPage::CurveTestPage(QWidget *parent) : QWidget(parent)
{
    setWindowTitle(tr("CurveTestPage"));
    setAttribute(Qt::WA_DeleteOnClose, true);

    m_curve = new Curve();
    m_curve->setTitle("Empty Bottle Test");
    m_curve->setXAxisName(tr("time(x50ms)"));
    m_curve->addGraph(tr("raw adc"), QColor(Qt::red));
    m_curve->addGraph(tr("max"), QColor(Qt::green));
    m_curve->addGraph(tr("delta"), QColor(Qt::blue));
    m_curve->addGraph(tr("delta2"), QColor(Qt::cyan));
    m_curve->setGeometry(1000, 200, 800, 600);
    m_curve->show();

    QVBoxLayout *vbl = new QVBoxLayout();
    vbl->addWidget(m_curve);
    setLayout(vbl);

//    Application::getInstance()->subscribe(EV_Test, this);
    Application::getInstance()->subscribe(EV_CurveTest, this);


    // test
    m_emptyBottleTest = new EmptyBottleTest(this);
    m_emptyBottleTest->start();

//    QTimer::singleShot(1000, this, [=](){
//        resize(800, 480);
//    });
}

CurveTestPage::~CurveTestPage()
{
    if (m_curve)
        delete m_curve;
    if (m_emptyBottleTest)
        delete m_emptyBottleTest;
}

void CurveTestPage::customEvent(QEvent *event)
{
    quint16 id = event->type();
    switch (id)
    {
        case EV_Test:
            qDebug() << "CurveTestPage EV_Test";
        break;
        case EV_CurveTest:
            if (m_curve != Q_NULLPTR)
            {
                CustomEvent *e = static_cast<CustomEvent*>(event);
                PlotXYParameter param = e->var.value<PlotXYParameter>();
                m_curve->onAddGraphPoint(param.num, param.x, param.y);
            }
        break;
        default:
        break;
    }
}

void CurveTestPage::closeEvent(QCloseEvent *event)
{
    Application::getInstance()->unsubscribe(EV_CurveTest, this);
    event->accept();
}
